<?php	
    $_helper = $this->helper('catalog/output');
	$this->addPriceBlockType('simple', 'catalog/product_price', 'catalog/product/price_product_list.phtml');
	$this->addPriceBlockType('configurable', 'catalog/product_price', 'catalog/product/price_product_list.phtml');
?>
<?php if($this->getRequest()->getParam('filter') && $this->getRequest()->getParam('filter') !== 'category'):?>
<div class="page-title">    
    <h1><?php echo $this->getTitle()?></h1>
</div>
<?php $_productCollection = $this->getProductCollection()?>
<?php if($_productCollection->count() == 0): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">    
<?php 
	$_inStockProduct = array();
	$_outStockProduct = array();
	//resort, sold out product last
	foreach ($_productCollection as $_product){
		if($_product->isSaleable()){
			$_inStockProduct[] = $_product;
		}else{
			$_outStockProduct[] = $_product;
		}
		// var_dump($_product->getId());
	}
	$_productCollection = array_merge($_inStockProduct, $_outStockProduct);
?>
<?php // Grid Mode ?>

<?php $_collectionSize = count($_productCollection) ?>
<?php $_columnCount = 3; ?>
<?php 
	$_bestsellerProductCollection = $this->helper('gtgobbler')->getBestsellerItems();
?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
	<?php if ($i++%$_columnCount==0): ?>
	<ul class="products-grid">
	<?php endif ?>
		<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>" id="productContainer_<?php echo $_product->getId()?>">
			<a id="productImgWrap_<?php echo $_product->getId()?>" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
				<img id="productImg_<?php echo $_product->getId()?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(223); ?>" width="223" height="220" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
			</a>
			<a class="quickview button" href="javascript:void(0);" id="quickviewBtn-<?php echo $_product->getId()?>">
				<span>quick view</span>
			</a>
			<?php 
				$isSoldOut = '';
				$isLimitedStock = '';
				$isBestSeller = '';
				$isNew = '';
			?>
			<?php if(!$_product->isSaleable()) $isSoldOut = ' sold-out' ?>
			<?php 
				$stockModel = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
				$stockQty = $stockModel->getStockQty();				
				$storeLimitStockQty = Mage::getStoreConfig('gobblersales/limitedstock/qty');
				$isFilterPage = false;
				if($filter = $this->getRequest()->getParam('filter')){
					if($filter == "limitedstock")
						$isFilterPage = true;
				}
				if($_product->isSaleable() && $stockQty > 0 && $stockQty < $storeLimitStockQty && !$isFilterPage)
					$isLimitedStock = ' limited-stock';
			
			?>		
			<?php				
				if(in_array($_product->getId(), $_bestsellerProductCollection))
					$isBestSeller = ' best-seller';
					
				if($this->isNewProduct($_product)){
					$isNew = ' new';
				}
			?>		
			<div class="product-status<?php echo $isSoldOut.$isLimitedStock.$isNew ?><?php if($this->getRequest()->getParam('filter') && $this->getRequest()->getParam('filter') !== 'bestseller') echo $isBestSeller?>"></div>
			<h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>                
			<?php echo $this->getPriceHtml($_product, true) ?>
			<div class="actions">
				<?php if($_product->isSaleable()): ?>					
					<div class="increase">
						<input type="text" readonly="readonly" id="qty-list-<?php echo $_product->getId()?>" value="1"/>
						<span class="up pointer" onclick="increaseQty(true, false, false, <?php echo $_product->getId()?>, <?php echo $this->helper('gtgobbler/catalog')->getMaxIncreaseQty($_product)?>)">up</span>
						<span class="down pointer" onclick="decreaseQty(true, false, false, <?php echo $_product->getId()?>)">down</span>
					</div>
					<?php if ($_product->getTypeInstance(true)->hasOptions($_product)):?>
						<button style="width: 168px;" id='mc-<?php echo $_product->getId()?>' class="button arrow-down mcbutton"><span><span>more choices</span></span></button>
						<?php $configurableAttributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product)?>
						<div class="select-flavour-container-list" id="flavourc-<?php echo $_product->getId()?>">
						<?php foreach($configurableAttributes as $attribute):?>							
							<table>
								<col />
								<col width="35"/>
								<tbody>
									<?php $f=1;$count=count($attribute['values']);
									foreach($attribute['values'] as $value):?>
										<?php
											$childProduct = $_product->getTypeInstance(true)->getProductByAttributes(array($attribute['attribute_id'] => $value['value_index']), $_product);
											$childProduct = Mage::getModel('catalog/product')->load($childProduct->getId());
											if(!$childProduct->isSaleable()) 
												continue;
										?>
										<tr <?php if($f == $count) echo 'class="last"'?> onclick="addToCartAjax(<?php echo $_product->getId()?>, true, false, false, <?php echo $attribute['attribute_id']?>, <?php echo $value['value_index']?>)">												
											<td class="label">													
												<?php echo $value['label']?>													
											</td>
											<td class="addto">
												<span>add to</span>
												<span class="big">cart</span>
											</td>												
										</tr>
									<?php $f++?>
									<?php endforeach?>
								</tbody>
							</table>							
						<?php endforeach?>
						</div>						
					<?php else:?>
						<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="addToCartAjax(<?php echo $_product->getId()?>, true, false, false)"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
						<?php $list = $_product->getAssociatedProducts();?>
						<div class="select-flavour-container-list" id="flavourc-<?php echo $_product->getId()?>">
							<table>
								<col />
								<col width="35"/>
								<tbody>
								
								</tbody>									
							</table>
						</div>
					<?php endif?>
				<?php else: ?>
					<p class="availability out-of-stock"><span><?php echo $this->__('Sold Out') ?></span></p>
				<?php endif; ?>
				<p class="added-notify" id="an-<?php echo $_product->getId()?>"></p>
			</div>			
		</li>
	<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
	</ul>
	<?php endif ?>
	<?php endforeach ?>
	<script type="text/javascript">
		decorateGeneric($$('ul.products-grid'), ['odd','even','first','last']);		
	</script>    
</div>
<?php endif; ?>
<?php else: //if filter is category?>
	<?php $categories = $this->getCategoryCollection()?>
	<?php if($categories && count($categories) > 0):?>		
		<?php foreach($categories as $category):?>			
			<?php $_productCollection = $this->getCategoryProducts($category)?>
			<?php if($_productCollection->getSize() == 0) continue; ?>
				
			<div class="page-title">    
				<h1><?php echo $category->getName()?></h1>
			</div>
			<div class="category-products">    
			<?php 
				$_inStockProduct = array();
				$_outStockProduct = array();
				//resort, sold out product last
				foreach ($_productCollection as $_product){
					if($_product->isSaleable()){
						$_inStockProduct[] = $_product;
					}else{
						$_outStockProduct[] = $_product;
					}
					// var_dump($_product->getId());
				}
				$_productCollection = array_merge($_inStockProduct, $_outStockProduct);
			?>
			<?php // Grid Mode ?>

			<?php $_collectionSize = count($_productCollection) ?>
			<?php $_columnCount = 3; ?>
			<?php 
				$_bestsellerProductCollection = $this->helper('gtgobbler')->getBestsellerItems();
			?>
			<?php $i=0; foreach ($_productCollection as $_product): ?>
				<?php if ($i++%$_columnCount==0): ?>
				<ul class="products-grid">
				<?php endif ?>
					<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>" id="productContainer_<?php echo $_product->getId()?>">
						<a id="productImgWrap_<?php echo $_product->getId()?>" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
							<img id="productImg_<?php echo $_product->getId()?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(223); ?>" width="223" height="220" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
						</a>
						<a class="quickview button" href="javascript:void(0);" id="quickviewBtn-<?php echo $_product->getId()?>">
							<span>quick view</span>
						</a>				
						<?php 
							$isSoldOut = '';
							$isLimitedStock = '';
							$isBestSeller = '';
							$isNew = '';
						?>
						<?php if(!$_product->isSaleable()) $isSoldOut = ' sold-out' ?>
						<?php 
							$stockModel = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
							$stockQty = $stockModel->getStockQty();				
							$storeLimitStockQty = Mage::getStoreConfig('gobblersales/limitedstock/qty');
							$isFilterPage = false;
							if($filter = $this->getRequest()->getParam('filter')){
								if($filter == "limitedstock")
									$isFilterPage = true;
							}
							if($_product->isSaleable() && $stockQty > 0 && $stockQty < $storeLimitStockQty && !$isFilterPage)
								$isLimitedStock = ' limited-stock';
						
						?>		
						<?php				
							if(in_array($_product->getId(), $_bestsellerProductCollection))
								$isBestSeller = ' best-seller';
								
							if($this->isNewProduct($_product)){
								$isNew = ' new';
							}
						?>		
						<div class="product-status<?php echo $isSoldOut.$isLimitedStock.$isNew ?><?php if($this->getRequest()->getParam('filter') && $this->getRequest()->getParam('filter') !== 'bestseller') echo $isBestSeller?>"></div>
						<h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>                
						<?php echo $this->getPriceHtml($_product, true) ?>
						<div class="actions">
							<?php if($_product->isSaleable()): ?>					
								<div class="increase">
									<input type="text" readonly="readonly" id="qty-list-<?php echo $_product->getId()?>" value="1"/>
									<span class="up pointer" onclick="increaseQty(true, false, false, <?php echo $_product->getId()?>, <?php echo $this->helper('gtgobbler/catalog')->getMaxIncreaseQty($_product)?>)">up</span>
									<span class="down pointer" onclick="decreaseQty(true, false, false, <?php echo $_product->getId()?>)">down</span>
								</div>
								<?php if ($_product->getTypeInstance(true)->hasOptions($_product)):?>
									<button style="width: 168px;" id='mc-<?php echo $_product->getId()?>' class="button arrow-down mcbutton"><span><span>more choices</span></span></button>
									<?php $configurableAttributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product)?>
									<div class="select-flavour-container-list" id="flavourc-<?php echo $_product->getId()?>">
									<?php foreach($configurableAttributes as $attribute):?>							
										<table>
											<col />
											<col width="35"/>
											<tbody>
												<?php $f=1;$count=count($attribute['values']);
												foreach($attribute['values'] as $value):?>
													<?php
														$childProduct = $_product->getTypeInstance(true)->getProductByAttributes(array($attribute['attribute_id'] => $value['value_index']), $_product);
														$childProduct = Mage::getModel('catalog/product')->load($childProduct->getId());
														if(!$childProduct->isSaleable()) 
															continue;
													?>
													<tr <?php if($f == $count) echo 'class="last"'?> onclick="addToCartAjax(<?php echo $_product->getId()?>, true, false, false, <?php echo $attribute['attribute_id']?>, <?php echo $value['value_index']?>)">												
														<td class="label">													
															<?php echo $value['label']?>													
														</td>
														<td class="addto">
															<span>add to</span>
															<span class="big">cart</span>
														</td>												
													</tr>
												<?php $f++?>
												<?php endforeach?>
											</tbody>
										</table>							
									<?php endforeach?>
									</div>						
								<?php else:?>
									<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="addToCartAjax(<?php echo $_product->getId()?>, true, false, false)"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
									<?php $list = $_product->getAssociatedProducts();?>
									<div class="select-flavour-container-list" id="flavourc-<?php echo $_product->getId()?>">
										<table>
											<col />
											<col width="35"/>
											<tbody>
											
											</tbody>									
										</table>
									</div>
								<?php endif?>
							<?php else: ?>
								<p class="availability out-of-stock"><span><?php echo $this->__('Sold Out') ?></span></p>
							<?php endif; ?>
							<p class="added-notify" id="an-<?php echo $_product->getId()?>"></p>
						</div>			
					</li>
				<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
				</ul>
				<?php endif ?>
				<?php endforeach ?>				 
			</div>			
		<?php endforeach?>
		<script type="text/javascript">
			decorateGeneric($$('ul.products-grid'), ['odd','even','first','last']);		
		</script> 
	<?php else:?>
		<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
	<?php endif?>
<?php endif?>